package com.tianbo.elfii.client.common;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;

/**  <pre>
     <b>类功能描述：</b><pre>
     题目计时器。
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonTopicTimer extends JPanel {
    int value=0;
    int maxValue=2000;

    BufferedImage whiteBackImg=null;
    BufferedImage redGlitterImg=null;

    boolean freezeValueMark;  //-冻结当前值标志， value等于0时可以解冻

    public void freezeValue(){
        freezeValueMark=true;
        repaint();
    }

    public ElfiiCommonTopicTimer(){
        File f;
        try {
            f=new File("resource-common/images/little time 01.png");
            whiteBackImg=ImageIO.read(f);
            f=new File("resource-common/images/little time 03.png");
            redGlitterImg=ImageIO.read(f);
        } catch(Exception e) {System.out.print(e);}
    }

    public void setValue(int value){
        if (value==0)
            freezeValueMark=false;
        if (freezeValueMark==false){
            this.value=value;
            if (value>maxValue)
                value=maxValue;
            repaint();
//            paintImmediately(0,0,getWidth(),getHeight());
        }
    }

    public int getValue(){
        return value;
    }

    public void setMaxValue(int maxValue){
        this.maxValue=maxValue;
    }

    public int getMaxValue(){
        return maxValue;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g ;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
        g2.setColor(Color.black);
        g2.fillRect(0,0,72,727);
        int baseX0=10;
        int baseY0=140;
        int totalLength=290;

        g2.drawImage(whiteBackImg,null,baseX0-15,baseY0-10);

        double d=(value+0.00001)/(maxValue+0.00001-400);
//        double d=(value)/(maxValue-400);
        int validLength=(int)(totalLength-totalLength*d);
        if (d>0.6666){
            g2.drawImage(redGlitterImg,null,baseX0-15,baseY0-10);
        }
        if (d!=0 && d>0.6666)
            g2.setColor(ElfiiCommonConstant.Elfii_Orange);
        else if(d<0.00001)
        	g2.setColor(Color.white);
        else
            g2.setColor(ElfiiCommonConstant.Elfii_Blue);
        g2.fillRoundRect(baseX0+4,baseY0+4+totalLength-validLength,18,validLength,15,15);
        if (d>0.99){
            g2.setStroke(new BasicStroke(3.0f));
            //g2.setColor(Color.red);
            //g2.drawRoundRect(baseX0,baseY0,25,300,15,15); 取消不用了
            if (value%200==0){
                g2.drawImage(redGlitterImg,null,baseX0-15,baseY0-10);
            }else{
                g2.drawImage(whiteBackImg,null,baseX0-15,baseY0-10);
                //g2.setColor(Color.black);
                //g2.fillRect(baseX0-5,baseY0-5,25+10,300+10);
            }
        }
    }

    public Dimension getPreferredSize() {
        return (new Dimension(80,727));
    }
}

